package com.treasure.mall.zbqmallapi;

import com.alibaba.fastjson.JSON;
import com.treasure.mall.biz.dto.user.AppLoginAsMobileDTO;
import com.treasure.mall.biz.dto.user.ModifyShopDTO;
import com.treasure.mall.biz.entity.embeddable.MerchantData;
import com.treasure.mall.biz.entity.product.CategoryPO;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.biz.service.core.product.CategoryService;
import com.treasure.mall.biz.service.core.user.MerchantService;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.biz.vo.user.UserVO;
import com.treasure.mall.zbq.common.RandomUtils;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.live.LiveEnums;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/4/15
 */

@RunWith(SpringRunner.class)
@ActiveProfiles(Constant.PROFILE_TEST)
@SpringBootTest(classes = ApiTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiTestBase {
    protected MockMvc mockMvc;
    @Autowired
    protected UserService userService;
    protected UserVO mockLoginUser;
    protected MerchantPO mockMerchant;
    @Autowired
    protected MerchantService merchantService;
    @Autowired
    protected CategoryService categoryService;
    protected List<CategoryPO> mockCategory = new ArrayList<>(3);

    @Resource
    private WebApplicationContext webApplicationContext;

    /**
     * 构建MockMvc
     */
    @Before
    public void init() {
        //初始化类目
        CategoryPO one = new CategoryPO();
        one.setCatName("一级");
        one.setParentCatId(0L);
        one.setSortNo(50);
        one.setCommissionRate(BigDecimal.TEN);
        mockCategory.add((CategoryPO) categoryService.addCategory(one).getData());
        CategoryPO two = new CategoryPO();
        two.setCatName("二级");
        two.setParentCatId(one.getCatId());
        two.setSortNo(50);
        mockCategory.add((CategoryPO) categoryService.addCategory(two).getData());
        CategoryPO three = new CategoryPO();
        three.setCatName("三级");
        three.setParentCatId(two.getCatId());
        three.setSortNo(50);
        mockCategory.add((CategoryPO) categoryService.addCategory(three).getData());

        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

        //注册一个用户
        AppLoginAsMobileDTO dto = new AppLoginAsMobileDTO();
        dto.setMobile(RandomUtils.randomMobile());
        mockLoginUser = userService.loginAsAppBindMobile(dto);

        //搞一个商家
        dto = new AppLoginAsMobileDTO();
        dto.setMobile(RandomUtils.randomMobile());
        UserVO mockMerchantUser = userService.loginAsAppBindMobile(dto);

        ModifyShopDTO modifyShopDTO = new ModifyShopDTO();
        modifyShopDTO.setCategoryId(mockCategory.get(2).getCatId());
        modifyShopDTO.setData(new MerchantData());
        mockMerchant = merchantService.applyShop(mockMerchantUser.getUserId(), modifyShopDTO);
        merchantService.modifyAuditStatus(mockMerchant.getId(), LiveEnums.AuditStatus.SUCCESS, null);

        //注册一个直播间

    }

    protected ApiResult parseApiResult(MvcResult mvcResult) throws UnsupportedEncodingException {
        String content = mvcResult.getResponse().getContentAsString();

        return JSON.parseObject(content, ApiResult.class);
    }
}
